<?php
class ContactController extends Core_Controller_Action
{
    public function indexmobileAction(){
        $this->_helper->layout()->setLayout("layout_mobile");
        $this->_indexaction(true);
    }

    public function indexAction() {
        if("mobile" == Core_MobileDetect::getCurrentDevice()){
            $this->indexmobileAction();
            $this->render("indexmobile");
        }
        else{
            $this->_indexaction();
        }
    }
    private function _indexaction($is_mobile = false) {
        require_once LIB_PATH . '/recaptchalib.php';
        $recaptcha_info = Core_Util::getConfig('recaptcha');
        $this->view->recaptcha_info = $recaptcha_info;
         if ( $this->getRequest()->isPost() ) {
            $is_valid = true;
            if ( !$is_mobile ) {
                $resp = recaptcha_check_answer ($recaptcha_info['private_key'],
                $_SERVER["REMOTE_ADDR"],
                $_POST["recaptcha_challenge_field"],
                $_POST["recaptcha_response_field"]);
                $is_valid = $resp->is_valid;
            }
            
            if (!$is_valid ){
                $this->view->message = array (
                    'status' => 0,
                    'message' => "The reCAPTCHA wasn't entered correctly"
                );
                $this->view->old_params = array (
                    'name' => $this->_getParam("name"),
                    'email' => $this->_getParam("email"),
                    'phone' => $this->_getParam("phone"),
                    'subject' => $this->_getParam("subject"),
                    'message' => $this->_getParam("message")
                );
            } else {
                $config_db = new Core_Model_DbTable_Config();
                $email_configs = $config_db->getMailSettings();
                $params = array (
                    'name' => $this->_getParam("name"),
                    'email' => $this->_getParam("email"),
                    'phone' => $this->_getParam("phone"),
                    'subject' => $this->_getParam("subject"),
                    'message' => $this->_getParam("message"),
                    'nameMail' => $this->_getParam('name') . ' - ' . $this->_getParam('phone'),
                    'to'       => $email_configs['smtp_user']

                );

                $this->_sendEmail($params);
                if(isset($_POST["send_me"]) && $_POST['send_me'] == 1){
                    $params['to'] = $this->_getParam("email");
                    $params['email'] = $email_configs['smtp_user'];
                    $params['nameMail'] = "Auto Reply";
                    $this->_sendEmail($params);
                }

                $this->view->message = array (
                    'status' => 1,
                    'message' => 'Your message was sent successfully. Thanks.'
                );
            }
        }
    }
    
    private function _sendEmail($params) {
        $headers = "From: {$params['name']} <{$params['email']}>";
        mail($params['to'], $params['subject'], $params['message'], $headers);
    }
}